<template>
  <!-- 24.0 文章详情页 -->
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />

    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ obj.title }}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="obj.aut_name" :label="obj.pubdate | dateformat">
        <template #icon>
          <img :src="obj.aut_photo" alt="" class="avatar" />
        </template>
        <template #default>
          <div>
            <van-button
              v-if="obj.is_followed"
              type="info"
              size="mini"
              @click="followedFn(false)"
              >已关注</van-button
            >
            <van-button
              v-else
              icon="plus"
              type="info"
              size="mini"
              plain
              @click="followedFn(true)"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content">{{ obj.content }}</div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button
          v-if="obj.attitude===1"
          icon="good-job"
          type="danger"
          size="small"
          @click="likeFn(-1)"
          >已点赞</van-button
        >
        <van-button
        v-else
        icon="good-job-o"
        type="danger"
        plain
        size="small"
        @click="likeFn(1)"
          >点赞</van-button
        >
      </div>
    </div>

    <!-- 25.0评论列表区域 -->
    <!-- 问什么obj.art_id报错undefined？因为网页打开，标签开始解析，此时obj还是空对象
    解决2种方式：
    1.路由上有id,:artId="$route.params.artid.toString()"
    2.如果数据需要等网络请求，为了避免取值报错，可以对当前标签用v-if修饰(有值了再更新渲染)
     -->
    <CommentList :artId="$route.params.artid"></CommentList>
  </div>
</template>

<script>
import { articleDetailAPI, followedAPI, unfollowedAPI, addLikeAPI, delLikeAPI } from '@/api'
import { Toast } from '@/ui/Toast'
import CommentList from './CommentList'
export default {
  data () {
    return {
      obj: {} // 保存的是文章数据，这次是对象
    }
  },
  methods: {
    async followedFn (bool) { // 24.2关注/取关(true-要关注，false-要取关)
      this.obj.is_followed = bool // 已实现前端，但一刷新又恢复，所以该调用接口
      if (bool) {
        const [err] = await followedAPI({
          target: this.obj.aut_id
        })
        if (err) Toast.fail('关注失败')
        else Toast.success('关注成功')
      } else {
        const [err] = await unfollowedAPI(this.obj.aut_id)
        if (err) Toast.fail('取消关注失败')
        else Toast.success('取消关注成功')
      }
    },
    // 24.4-点赞
    async likeFn (num) {
      this.obj.attitude = num
      if (num === 1) { // 要点赞
        const [err] = await addLikeAPI({
          target: this.obj.art_id.toString()
        })
        if (err) Toast.fail('点赞失败')
        else Toast.success('点赞成功')
      } else { // 要取消点赞
        const [err] = await delLikeAPI(this.obj.art_id.toString())
        if (err) Toast.fail('取消点赞失败')
        else Toast.success('取消点赞成功')
      }
    }
  },

  async created () {
    // 24.1 artid文章详情里路由设置的
    const [, res] = await articleDetailAPI(this.$route.params.artid)
    // console.log(res)
    this.obj = res.data.data
  },
  // 25.0
  components: {
    CommentList
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
